package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.UserLoginRequest;
import net.xdclass.request.UserRegisterRequest;
import net.xdclass.service.FileService;
import net.xdclass.service.UserService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-03-29
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {


    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     * 上传用户头像
     * 默认最大为1M
     * @param file  文件
     * @return
     */
    @ApiOperation("用户上传头像")
    @PostMapping(value = "upload")
    public JsonData uploadUserImg(
            @ApiParam(value = "文件上传",required = true)
            @RequestPart("file") MultipartFile file){
        String img = fileService.upLoadUserImg(file);
        return img == null?JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL):JsonData.buildSuccess(img);

    }

    @ApiOperation("用户注册")
    @PostMapping(value = "register")
    public JsonData register(
            @ApiParam("用户注册对象")
            @RequestBody UserRegisterRequest userRegisterRequest){
        return  userService.register(userRegisterRequest);
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(@ApiParam("用户登录对象") @RequestBody UserLoginRequest userLoginRequest){
        return userService.login(userLoginRequest);
    }

    /**
     * 用户个人信息查询
     * @return
     */
    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detil(){
        UserVO userVo = userService.findUserDetil();
        return JsonData.buildSuccess(userVo);
    }

}

